package com.suse.springtransdemo.controller;


import com.suse.springtransdemo.sevice.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/trans")
public class TransController {

    @Autowired
    private UserService userService;

    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r1")
    public Boolean registry(String name,String password) {

        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数："+result);

        return true;
    }

    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r2")
    public Boolean r2(String name,String password) throws IOException {

        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数："+result);

        if(true) {
            throw new IOException();
        }
        return true;
    }
}
